/* model */
import { wsEnvMap } from '@src/component/business/BizWebSocket/model';
/* util */
// @ts-ignore
import md5 from 'js-md5';
import { getRootWindowInitData } from '@src/util/window';
import { isDevelopment } from '@src/util/platform';
import { userCenterGetTokenInfo } from '@src/util/userCenter';
import { config } from '@server/util/NodeProxyConfig'


function getProtocol() {
  const env = getEnv()
  
  return wsEnvMap[env] ? 'wss' : 'ws';
}

function getEnv(): string {
  const rootWindowInitData = getRootWindowInitData()
  return rootWindowInitData.env || ''
}

function getSign(timestamp: number, userId: string ): string {
  const signString = `ts=${timestamp}&userId=${userId}SHOU_HOU_BAO`;
  return md5(signString);
}

function getWebSocketUrl(url: string) {
  
  const protocol = getProtocol()
  // @ts-ignore
  const token = userCenterGetTokenInfo()?.token || localStorage.getItem('_developmentToken') || ''
  
  const env = getEnv()
  const hostName = window.location.hostname || wsEnvMap[env]?.hostname
  
  // 如果是本地启动 代理至nodeProxy
  if((window as any).location.port != ''){
    return `ws://127.0.0.1:${config.wsPort}${url}`
  }
  
  return `${protocol}://${hostName}${url}/${token}`
  
}

export {
  getWebSocketUrl
}
